import type { balanceType, locationType, userInfo } from '@/types/component'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useMemberStore = defineStore(
  'member',
  () => {
    /**
     * * token
     */
    const token = ref<string>()

    /**
     * * 保存token
     * @param val  string
     */
    const setToken = (val: string) => {
      token.value = val
    }

    /**
     * * 删除token
     */
    const clearToken = () => {
      token.value = undefined
    }

    /**
     * * 用户个人信息
     */
    const userInfo = ref<userInfo>()

    /**
     * * 保存会员信息
     * @param val
     */
    const setUserInfo = (val: userInfo) => {
      userInfo.value = val
    }

    /**
     * *  清理会员信息，退出时使用
     */
    const clearUserInfo = () => {
      userInfo.value = undefined
      // console.log(userInfo.value)
    }

    /**
     * * 用户经纬度信息
     */
    const location = ref<locationType>()

    /**
     * * 保存用户经纬度信息，获取设备列表使用
     * @param val  传入经纬度
     */
    const setLocation = (val: locationType) => {
      location.value = val
    }

    /**
     * * 清理用户经纬度信息，退出时使用
     */
    const clearLocation = () => {
      location.value = undefined
    }


    //  储值数据

    const balanceInfo = ref<balanceType>()

    // 保存储值信息
    const setBalanceInfo = (val: balanceType) => {
      balanceInfo.value = val
    }
    // 清除储值信息
    const clearBalanceInfo = (val: balanceType) => {
      balanceInfo.value = val
    }

    // 免密支付
    const hasPayPasswordFree = ref<number>()

    const setHasPayPasswordFree = (val: number) => {
      hasPayPasswordFree.value = val
    }

    // 支付密码
    const hasPayPassword = ref<number>()

    const setHasPayPassword = (val: number) => {
      hasPayPassword.value = val
    }

    return {
      // token
      token,
      setToken,
      clearToken,
      // 用户数据
      userInfo,
      setUserInfo,
      clearUserInfo,
      // 定位信息
      location,
      setLocation,
      clearLocation,
      // 储值信息
      balanceInfo,
      setBalanceInfo,
      clearBalanceInfo,
      hasPayPasswordFree,
      setHasPayPasswordFree,
      hasPayPassword,
      setHasPayPassword,
    }
  },

  {
    // 数据持久化
    persist: {
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
        getItem(key) {
          return uni.getStorageSync(key)
        },
      },
    },
  },
)
